// @algorithm @lc id=621 lang=cpp 
// @title task-scheduler

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        unordered_map<char,int> execTimes;
        for(auto & t : tasks)
            execTimes[t]++;
        int maxTimes = max_element(execTimes.begin(), execTimes.end(),
            [](const auto&a, const auto&b){return a.second<b.second;})->second;
        int maxCount = accumulate(execTimes.begin(), execTimes.end(), 0,
            [=](int acc, const auto&a){ return acc + (a.second==maxTimes);});
        return max( (int)tasks.size(), (maxTimes-1)*(n+1)+maxCount );
    }
};